package com.wctj.api.business.cmb.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wctj.api.base.Result;
import com.wctj.api.base.entity.DdwxPaymentMessageInfo;
import com.wctj.api.base.entity.DdwxPaymentRecord;
import com.wctj.api.base.enums.*;
import com.wctj.api.base.response.Response;
import com.wctj.api.base.utils.HttpClientUtil;
import com.wctj.api.base.utils.JacksonUtil;
import com.wctj.api.base.utils.JsonUtils;
import com.wctj.api.business.cmb.dto.CMBBatchTransferDTO;
import com.wctj.api.business.cmb.dto.CMBBatchTransferDetailDTO;
import com.wctj.api.business.cmb.dto.CMBSingleTransferDTO;
import com.wctj.api.business.cmb.dto.WithdrawalSummaryDTO;
import com.wctj.api.business.cmb.enums.CmbPayStatusEnums;
import com.wctj.api.business.cmb.service.CmbPayManagerService;
import com.wctj.api.business.common.dto.*;
import com.wctj.api.business.common.service.DdwxPaymentCfgInfoService;
import com.wctj.api.business.common.service.DdwxPaymentMessageInfoService;
import com.wctj.api.business.common.service.DdwxPaymentRecordService;
import com.wctj.api.business.common.utils.ValidatorUtil;
import com.wctj.api.infra.common.util.sign.SignMd5Utils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class CmbPayManagerServiceImpl implements CmbPayManagerService {
    @Resource
    private CmbAssistant cmbAssistant;
    @Resource
    protected DdwxPaymentMessageInfoService ddwxPaymentMessageInfoService;
    @Resource
    protected DdwxPaymentRecordService ddwxPaymentRecordService;
    @Value("${paysystem.cmb.phpCallbackBizCode:60000}")
    private String phpCallbackBizCode;
    @Resource
    private DdwxPaymentCfgInfoService ddwxPaymentCfgInfoService;

    private static final Logger log = LoggerFactory.getLogger(CmbPayManagerServiceImpl.class);

    @Override
    public Result<Object> toWithdrawal(WithdrawalOrderDTO withdrawalOrderDTO, String payOrderNum) {
        WithdrawalSummaryDTO withdrawalSummaryDTO;
        JSONObject body = new JSONObject( withdrawalOrderDTO.getBody());
        String ttlamt = body.optString("ttlamt");
        try {
            withdrawalSummaryDTO = JacksonUtil.parse(body.toString(), WithdrawalSummaryDTO.class);
        } catch (Exception e) {
            log.error("招行提现订单生成失败", e);
            return Result.fail(ApiCode.PAY_WITHDRAWAL_ORDER_FAIL, body.toString());
        }
        ValidatorUtil.validate(withdrawalSummaryDTO);
        DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO = ddwxPaymentCfgInfoService.queryInfoByBizCodeAndPlatform(phpCallbackBizCode,withdrawalOrderDTO.getSystemPlatform());
        Response response = cmbAssistant.withdrawal(withdrawalSummaryDTO, payOrderNum,ddwxPaymentCfgInfoDTO);
        if (response.isSuccess()){
            Object data = response.getData();
            // 提现信息存储
            savePaymentInfo(withdrawalOrderDTO,data,payOrderNum,new BigDecimal(ttlamt),ddwxPaymentCfgInfoDTO);
            Map<String,String> resMap = new HashMap<>();
            resMap.put("msg","提现动作完成!");
            resMap.put("payOrderNum",payOrderNum);
            return Result.success(resMap);
        }else {
            return Result.fail(ApiCode.PAY_WITHDRAWAL_ORDER_FAIL, response.getMessage());
        }
    }

    /**
     * 单笔支付转账
     * @param singleTransferDTO
     * @param payOrderNum
     * @return
     */
    @Override
    public Result<Object> toSingleTransfer(SingleTransferDTO singleTransferDTO, String payOrderNum) {
        CMBSingleTransferDTO cmbSingleTransferDTO;
        JSONObject body = new JSONObject( singleTransferDTO.getBody());
        try {
            cmbSingleTransferDTO = JacksonUtil.parse(body.toString(), CMBSingleTransferDTO.class);
        } catch (Exception e) {
            log.error("招行单笔支付转账失败", e);
            return Result.fail(ApiCode.PAY_SINGLETRANSFER_ORDER_FAIL, body.toString());
        }
        ValidatorUtil.validate(cmbSingleTransferDTO);
        DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO = ddwxPaymentCfgInfoService.queryInfoByBizCodeAndPlatform(phpCallbackBizCode,singleTransferDTO.getSystemPlatform());
        Response response = cmbAssistant.SingleTransfer(cmbSingleTransferDTO, payOrderNum,ddwxPaymentCfgInfoDTO);
        if (response.isSuccess()){
            Object data = response.getData();
            // 单笔支付转账信息存储
            saveSingleTransferPaymentInfo(singleTransferDTO,data,payOrderNum,new BigDecimal(cmbSingleTransferDTO.getTrsAmt()),ddwxPaymentCfgInfoDTO);
            Map<String,String> resMap = new HashMap<>();
            resMap.put("msg","单笔支付转账动作完成!");
            resMap.put("payOrderNum",payOrderNum);
            return Result.success(resMap);
        }else {
            return Result.fail(ApiCode.PAY_SINGLETRANSFER_ORDER_FAIL, response.getMessage());
        }
    }

    @Transactional(rollbackFor = {Exception.class})
    public void saveSingleTransferPaymentInfo(SingleTransferDTO singleTransferDTO, Object data, String payOrderNum, BigDecimal bigDecimal, DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        String payNum = "";
        //JSONObject body  = JSONObject.parseObject(data.toString()).getJSONObject("body");
        //JSONObject body  =JacksonUtil.parse(data.toString(),JSONObject.class);
        JSONObject body = JsonUtils.convertValue(data.toString(), JSONObject.class).optJSONObject("body");
        JSONArray bb1payopz1 = body.optJSONArray("bb1payopz1");
        try {
            if (bb1payopz1.length()>0){
                payNum = bb1payopz1.getJSONObject(0).getString("reqNbr");
            }
        } catch (Exception e) {
            log.error("org.json.JSONArray 获取JSONArray对象异常", e);
            throw new RuntimeException(e);
        }

        DdwxPaymentRecord paymentRecord = new DdwxPaymentRecord();
        paymentRecord.setPayOrderNum(payOrderNum);
        paymentRecord.setBizCode(phpCallbackBizCode);//cmb
        paymentRecord.setPlatform(PayChannelEnums.ADAPAY_CMBPAY.getType().name());
        paymentRecord.setBizOrderNum(payOrderNum); //业务流水号
        paymentRecord.setPayChannel(PayChannelEnums.ADAPAY_CMBPAY.getCode());
        paymentRecord.setPayNum(payNum);
        paymentRecord.setMoney(bigDecimal);
        // 处理中
        paymentRecord.setStatus(CmbPayStatusEnums.pending.getCode());
        paymentRecord.setVersion(0);
        paymentRecord.setPayType(PayBusinessTypeEnums.TRANSFER.name());
        // save db
        ddwxPaymentRecordService.save(paymentRecord);

        // message db
        DdwxPaymentMessageInfoDTO msgInfo = new DdwxPaymentMessageInfoDTO();
        msgInfo.setBizCode(phpCallbackBizCode);
        msgInfo.setEventType(EventTypeEnums.YQN02030.getType());
        msgInfo.setPayOrderNum(payOrderNum);
        msgInfo.setPushStatus(CmbPayStatusEnums.pending.getCode());
        msgInfo.setStatus(CmbPayStatusEnums.pending.getCode());
        msgInfo.setNotifyUrl(Objects.isNull(ddwxPaymentCfgInfoDTO) ? "" : ddwxPaymentCfgInfoDTO.getNotifyUrl());
        msgInfo.setRequestBody(JsonUtils.toJson(singleTransferDTO));
        msgInfo.setResponseMsg(data.toString());
        msgInfo.setPlatform(PayPlatformEnums.CMB_PAY.name());
        saveMessage(msgInfo);
    }


    /**
     * 批量支付转账
     * @param batchTransferDTO
     * @param payOrderNum
     * @return
     */
    @Override
    public Result<Object> toBatchTransfer(BatchTransferDTO batchTransferDTO, String payOrderNum) {
        CMBBatchTransferDTO cmbBatchTransferDTO;
        JSONObject body = new JSONObject( batchTransferDTO.getBody());
        try {
            cmbBatchTransferDTO = JacksonUtil.parse(body.toString(), CMBBatchTransferDTO.class);
        } catch (Exception e) {
            log.error("招行批量支付转账失败", e);
            return Result.fail(ApiCode.PAY_BATCHTRANSFER_ORDER_FAIL, body.toString());
        }
        ValidatorUtil.validate(cmbBatchTransferDTO);
        // 请求体内 批量支付数组进行入参校验
        for (CMBBatchTransferDetailDTO cmbBatchTransferDetailDTO : cmbBatchTransferDTO.getCmbBatchTransferDetailDTOList()){
            ValidatorUtil.validate(cmbBatchTransferDetailDTO);
        }
        DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO = ddwxPaymentCfgInfoService.queryInfoByBizCodeAndPlatform(phpCallbackBizCode,batchTransferDTO.getSystemPlatform());
        Response response = cmbAssistant.BatchTransfer(cmbBatchTransferDTO, payOrderNum,ddwxPaymentCfgInfoDTO);
        if (response.isSuccess()){
            Object data = response.getData();
            // 批量支付转账信息存储
            saveBatchTransferPaymentInfo(batchTransferDTO,data,payOrderNum,new BigDecimal(cmbBatchTransferDTO.getTtlamt()),ddwxPaymentCfgInfoDTO);
            Map<String,String> resMap = new HashMap<>();
            resMap.put("msg","批量支付转账动作完成!");
            resMap.put("payOrderNum",payOrderNum);
            return Result.success(resMap);
        }else {
            return Result.fail(ApiCode.PAY_BATCHTRANSFER_ORDER_FAIL, response.getMessage());
        }
    }

    @Transactional(rollbackFor = {Exception.class})
    public void saveBatchTransferPaymentInfo(BatchTransferDTO batchTransferDTO, Object data, String payOrderNum, BigDecimal bigDecimal, DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        String payNum = "";
        //JSONObject body  = JSONObject.parseObject(data.toString()).getJSONObject("body");
        //JSONObject body  =JacksonUtil.parse(data.toString(),JSONObject.class);
        JSONObject body = JsonUtils.convertValue(data.toString(), JSONObject.class).optJSONObject("body");
        JSONArray bb1paybhz1 = body.optJSONArray("bb1paybhz1");
        try {
            if (bb1paybhz1.length()>0){
                payNum = bb1paybhz1.getJSONObject(0).getString("bthNbr");
            }
        } catch (Exception e) {
            log.error("org.json.JSONArray 获取JSONArray对象异常", e);
            throw new RuntimeException(e);
        }

        DdwxPaymentRecord paymentRecord = new DdwxPaymentRecord();
        paymentRecord.setPayOrderNum(payOrderNum);
        paymentRecord.setBizCode(phpCallbackBizCode);//cmb
        paymentRecord.setPlatform(PayChannelEnums.ADAPAY_CMBPAY.getType().name());
        paymentRecord.setBizOrderNum(payOrderNum); //业务流水号
        paymentRecord.setPayChannel(PayChannelEnums.ADAPAY_CMBPAY.getCode());
        paymentRecord.setPayNum(payNum);
        paymentRecord.setMoney(bigDecimal);
        // 处理中
        paymentRecord.setStatus(CmbPayStatusEnums.pending.getCode());
        paymentRecord.setVersion(0);
        paymentRecord.setPayType(PayBusinessTypeEnums.TRANSFER.name());
        // save db
        ddwxPaymentRecordService.save(paymentRecord);

        // message db
        DdwxPaymentMessageInfoDTO msgInfo = new DdwxPaymentMessageInfoDTO();
        msgInfo.setBizCode(phpCallbackBizCode);
        msgInfo.setEventType(EventTypeEnums.YQN02030.getType());
        msgInfo.setPayOrderNum(payOrderNum);
        msgInfo.setPushStatus(CmbPayStatusEnums.pending.getCode());
        msgInfo.setStatus(CmbPayStatusEnums.pending.getCode());
        msgInfo.setNotifyUrl(Objects.isNull(ddwxPaymentCfgInfoDTO) ? "" : ddwxPaymentCfgInfoDTO.getNotifyUrl());
        msgInfo.setRequestBody(JsonUtils.toJson(batchTransferDTO));
        msgInfo.setResponseMsg(data.toString());
        msgInfo.setPlatform(PayPlatformEnums.CMB_PAY.name());
        saveMessage(msgInfo);
    }


    @Transactional(rollbackFor = {Exception.class})
    public void savePaymentInfo(WithdrawalOrderDTO withdrawalOrderDTO, Object data, String payOrderNum, BigDecimal ttlamt,DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO) {
        String payNum = "";
        //JSONObject body  = JSONObject.parseObject(data.toString()).getJSONObject("body");
        //JSONObject body  =JacksonUtil.parse(data.toString(),JSONObject.class);
        JSONObject body = JsonUtils.convertValue(data.toString(), JSONObject.class).optJSONObject("body");
        JSONArray bb6cdcbhz1 = body.optJSONArray("bb6cdcbhz1");
        try {
            if (bb6cdcbhz1.length()>0){
                payNum = bb6cdcbhz1.getJSONObject(0).getString("reqnbr");
            }
        } catch (Exception e) {
            log.error("org.json.JSONArray 获取JSONArray对象异常", e);
            throw new RuntimeException(e);
        }

        DdwxPaymentRecord paymentRecord = new DdwxPaymentRecord();
        paymentRecord.setPayOrderNum(payOrderNum);
        paymentRecord.setBizCode(phpCallbackBizCode);//cmb
        paymentRecord.setPlatform(PayChannelEnums.ADAPAY_CMBPAY.getType().name());
        paymentRecord.setBizOrderNum(payOrderNum); //业务流水号
        paymentRecord.setPayChannel(PayChannelEnums.ADAPAY_CMBPAY.getCode());
        paymentRecord.setPayNum(payNum);
        paymentRecord.setMoney(ttlamt);
        // 处理中
        paymentRecord.setStatus(CmbPayStatusEnums.pending.getCode());
        paymentRecord.setVersion(0);
        paymentRecord.setPayType(PayBusinessTypeEnums.WITHDRAWAL.name());
        // save db
        ddwxPaymentRecordService.save(paymentRecord);


        // message db
        DdwxPaymentMessageInfoDTO msgInfo = new DdwxPaymentMessageInfoDTO();
        msgInfo.setBizCode(phpCallbackBizCode);
        msgInfo.setEventType(EventTypeEnums.YQN03010.getType());
        msgInfo.setPayOrderNum(payOrderNum);
        msgInfo.setPushStatus(CmbPayStatusEnums.pending.getCode());
        msgInfo.setStatus(CmbPayStatusEnums.pending.getCode());
        msgInfo.setNotifyUrl(Objects.isNull(ddwxPaymentCfgInfoDTO) ? "" : ddwxPaymentCfgInfoDTO.getNotifyUrl());
        msgInfo.setRequestBody(JsonUtils.toJson(withdrawalOrderDTO));
        msgInfo.setResponseMsg(data.toString());
        msgInfo.setPlatform(PayPlatformEnums.CMB_PAY.name());
        saveMessage(msgInfo);
    }

    protected Boolean saveMessage(DdwxPaymentMessageInfoDTO infoDTO) {
        // message db
        DdwxPaymentMessageInfo msgInfo = new DdwxPaymentMessageInfo();
        msgInfo.setBizCode(infoDTO.getBizCode());
        msgInfo.setPayOrderNum(infoDTO.getPayOrderNum());
        msgInfo.setPushStatus(infoDTO.getPushStatus());
        msgInfo.setStatus(infoDTO.getStatus());
        msgInfo.setVersion(0);
        msgInfo.setPushRetryCnt(0);
        msgInfo.setNotifyUrl(infoDTO.getNotifyUrl());
        msgInfo.setRequestBody(infoDTO.getRequestBody());
        msgInfo.setResponseMsg(infoDTO.getResponseMsg());
        msgInfo.setEventType(infoDTO.getEventType());
        msgInfo.setPlatform(infoDTO.getPlatform());
        return ddwxPaymentMessageInfoService.save(msgInfo);
    }

    /**
     * 获取 账号详细信息
     * @param accountDTO
     * @return
     */
    @Override
    public Result<Object> getAccountDetail(AccountDTO accountDTO) {
        DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO = ddwxPaymentCfgInfoService.queryInfoByBizCodeAndPlatform(phpCallbackBizCode,accountDTO.getSystemPlatform());
        Response response = cmbAssistant.getAccountDetail(accountDTO,ddwxPaymentCfgInfoDTO);
        if (response.isSuccess()){
            /**
             * {
             *  "response":{
             *          "body":{
             *                  "ntqacinfz":[
             *                      {
             *                          "accblv":"14246.45",
             *                          "accitm":"10001",
             *                          "accnam":"银企直连测试用户专用09",
             *                          "accnbr":"128964530610006",
             *                          "avlblv":"14246.48","bbknbr":"75","ccynbr":"10","dpstxt":"活期","hldblv":"0.00","intrat":"3.2500000","lmtovr":"0.00","mutdat":"99991231","onlblv":"14246.48","opnbbk":"755","opnbrn":"755512","opndat":"20240516","shlflg":"N","stscod":"A"
             *                          }
             *                          ]
             *                  },
             *                  "head":{
             *                      "bizcode":"","funcode":"NTQACINF","reqid":"2024101711230609836787044","resultcode":"SUC0000","resultmsg":"","rspid":"202410171123067360001cdcserver-6485f7bb67-zcmt4","userid":"U006763991"
             *                      }
             *              },
             *  "signature":{
             *          "sigdat":"RymM7JUtcDXDoxXSP8u+6+IM3n+FGWUm7F1AR1Zr5BJmudtpyD9QKdpeislQZYcqwwAVBhRWtswfrCmkUx5lTQ==","sigtim":"20241017112306"
             *          }
             *
             * }
             */
            Object data = response.getData();
            //JSONObject body  =JacksonUtil.parse(data.toString(),JSONObject.class);
            JSONObject body = JsonUtils.convertValue(data.toString(), JSONObject.class).optJSONObject("body");
            JSONArray ntqacinfz = body.optJSONArray("ntqacinfz");
            JSONObject AccountDetailObj = new JSONObject();
            try {
                if (ntqacinfz.length()>0){
                    AccountDetailObj = ntqacinfz.getJSONObject(0);
                }
            } catch (Exception e) {
                log.error("org.json.JSONArray 获取JSONArray对象异常", e);
                throw new RuntimeException(e);
            }
            return Result.success(AccountDetailObj.toString());
        }else {
            return Result.fail(ApiCode.PAY_CMB_BACK_MSG_ERROR, response.getMessage());
        }
    }

    /**
     * 查询交易信息列表
     * @param transactionDTO
     * @return
     */
    @Override
    public Result<Object> getTransactionList(TransactionDTO transactionDTO) {
        DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO = ddwxPaymentCfgInfoService.queryInfoByBizCodeAndPlatform(phpCallbackBizCode,transactionDTO.getSystemPlatform());
        Response response = cmbAssistant.getTransactionList(transactionDTO,ddwxPaymentCfgInfoDTO);
        if (response.isSuccess()){
            /**
             * {"response":{
             *          "body":{
             *                  "TRANSQUERYBYBREAKPOINT_Y1":[
             *                                  {"acctNbr":"128964530680006","expectNextSequence":"3","transDate":"20241014"}
             *                                  ],
             *                  "TRANSQUERYBYBREAKPOINT_Z1":[
             *                                  {"creditAmount":"0.00","creditNums":"0","ctnFlag":"N","debitAmount":"-507.40","debitNums":"16","queryAcctNbr":"128964530680006"}
             *                                  ],
             *                  "TRANSQUERYBYBREAKPOINT_Z2":[
             *                                  {"acctOnlineBal":"14713.85","billNumber":"                    ","businessName":"代发其他","businessText":" ","ctpAcctNbr":" ","ctpBankAddress":" ","ctpBankName":" ","currencyNbr":"10","extendedRemark":"F001UQK4ZK          ","fatOrSonAccount":" ","fatOrSonBankAddress":" ","fatOrSonBankName":" ","fatOrSonCompanyName":" ","infoFlag":"1","loanCode":"D","mchOrderNbr":"                                                  ","remarkTextClt":"2024-10-11 代发批量支付经办最新一次调试","requestNbr":"4C20002130","reversalFlag":" ","textCode":"AGSH","transAmount":"-40.00","transDate":"20241010","transSequenceIdn":"C0146ZD000095XZ","transTime":"094415","valueDate":"20241010","virtualNbr":" ","yurRef":"202410101844192091375386625"},
             *                                  {"acctOnlineBal":"14598.35","billNumber":"                    ","businessName":"支付","businessText":" ","ctpAcctName":"银企直连测试用户专用09","ctpAcctNbr":"1289645306100070000001","ctpBankAddress":"招商银行","ctpBankName":"招商银行","currencyNbr":"10","extendedRemark":"06S136ZB000001001080","fatOrSonAccount":" ","fatOrSonBankAddress":" ","fatOrSonBankName":" ","fatOrSonCompanyName":" ","infoFlag":"1","loanCode":"D","mchOrderNbr":"                                                  ","remarkTextClt":"用途 测试转账账号子单元","requestNbr":"7782007665","reversalFlag":" ","textCode":"CPAA","transAmount":"-115.50","transDate":"20241010","transSequenceIdn":"C0146ZD0001NW1Z","transTime":"134351","valueDate":"20241010","virtualNbr":" ","yurRef":"202410101110001"},
             *                                  {"acctOnlineBal":"14482.85","billNumber":"                    ","businessName":"支付","businessText":" ","ctpAcctName":"银企直连测试用户专用09","ctpAcctNbr":"1289645306100070000001","ctpBankAddress":"招商银行","ctpBankName":"招商银行","currencyNbr":"10","extendedRemark":"06S136ZB000001001085","fatOrSonAccount":" ","fatOrSonBankAddress":" ","fatOrSonBankName":" ","fatOrSonCompanyName":" ","infoFlag":"1","loanCode":"D","mchOrderNbr":"                                                  ","remarkTextClt":"用途 测试转账账号子单元","requestNbr":"7782007666","reversalFlag":" ","textCode":"CPAA","transAmount":"-115.50","transDate":"20241010","transSequenceIdn":"C0146ZD0000JQTZ","transTime":"135117","valueDate":"20241010","virtualNbr":" ","yurRef":"202410101110002"},
             *                                  {"acctOnlineBal":"14442.85","billNumber":"                    ","businessName":"代发其他","businessText":" ","ctpAcctNbr":" ","ctpBankAddress":" ","ctpBankName":" ","currencyNbr":"10","extendedRemark":"F001UQL4ZK          ","fatOrSonAccount":" ","fatOrSonBankAddress":" ","fatOrSonBankName":" ","fatOrSonCompanyName":" ","infoFlag":"1","loanCode":"D","mchOrderNbr":"                                                  ","remarkTextClt":"2024-10-11 代发批量支付经办最新一次调试","requestNbr":"4C20002132","reversalFlag":" ","textCode":"AGSH","transAmount":"-40.00","transDate":"20241010","transSequenceIdn":"C0146ZD0000JSRZ","transTime":"135215","valueDate":"20241010","virtualNbr":" ","yurRef":"202410101844254582834970625"},
             *                                  {"acctOnlineBal":"14327.35","billNumber":"                    ","businessName":"支付","businessText":" ","ctpAcctName":"银企直连测试用户专用09","ctpAcctNbr":"1289645306100074893822","ctpBankAddress":"招商银行","ctpBankName":"招商银行","currencyNbr":"10","extendedRemark":"06S136ZB000001001195","fatOrSonAccount":" ","fatOrSonBankAddress":" ","fatOrSonBankName":" ","fatOrSonCompanyName":" ","infoFlag":"1","loanCode":"D","mchOrderNbr":"                                                  ","remarkTextClt":"用途 测试转账账号子单元","requestNbr":"7782007667","reversalFlag":" ","textCode":"CPAA","transAmount":"-115.50","transDate":"20241010","transSequenceIdn":"C0146ZD0000L3MZ","transTime":"142532","valueDate":"20241010","virtualNbr":" ","yurRef":"202410101110003"},
             *                                  {"acctOnlineBal":"14327.25","billNumber":"                    ","businessName":"支付","businessText":" ","ctpAcctName":"银企直连测试用户专用09","ctpAcctNbr":"1289645306100074893822","ctpBankAddress":"招商银行","ctpBankName":"招商银行","currencyNbr":"10","extendedRemark":"06S136ZB000001001295","fatOrSonAccount":" ","fatOrSonBankAddress":" ","fatOrSonBankName":" ","fatOrSonCompanyName":" ","infoFlag":"1","loanCode":"D","mchOrderNbr":"                                                  ","remarkTextClt":"用途 测试转账账号子单元","requestNbr":"7782007668","reversalFlag":" ","textCode":"CPAA","transAmount":"-0.10","transDate":"20241010","transSequenceIdn":"C0146ZD0000L85Z","transTime":"142930","valueDate":"20241010","virtualNbr":" ","yurRef":"202410101110004"},
             *                                  {"acctOnlineBal":"14327.15","billNumber":"                    ","businessName":"支付","businessText":" ","ctpAcctName":"银企直连测试用户专用09","ctpAcctNbr":"1289645306100074893822","ctpBankAddress":"招商银行","ctpBankName":"招商银行","currencyNbr":"10","extendedRemark":"06S136ZB000001001724","fatOrSonAccount":" ","fatOrSonBankAddress":" ","fatOrSonBankName":" ","fatOrSonCompanyName":" ","infoFlag":"1","loanCode":"D","mchOrderNbr":"                                                  ","remarkTextClt":"用途 测试转账账号子单元","requestNbr":"7782008729","reversalFlag":" ","textCode":"CPAA","transAmount":"-0.10","transDate":"20241010","transSequenceIdn":"C0146ZD0001Q5ZZ","transTime":"144616","valueDate":"20241010","virtualNbr":" ","yurRef":"202410101110005"},
             *                                  {"acctOnlineBal":"14327.05","billNumber":"                    ","businessName":"支付","businessText":" ","ctpAcctName":"银企直连测试用户专用09","ctpAcctNbr":"1289645306100074893822","ctpBankAddress":"招商银行","ctpBankName":"招商银行","currencyNbr":"10","extendedRemark":"06S136ZB000002001706","fatOrSonAccount":" ","fatOrSonBankAddress":" ","fatOrSonBankName":" ","fatOrSonCompanyName":" ","infoFlag":"1","loanCode":"D","mchOrderNbr":"                                                  ","remarkTextClt":"用途 测试转账账号子单元","requestNbr":"7782008730","reversalFlag":" ","textCode":"CPAA","transAmount":"-0.10","transDate":"20241010","transSequenceIdn":"C0146ZD0001Q61Z","transTime":"144617","valueDate":"20241010","virtualNbr":" ","yurRef":"202410101110006"},
             *                                  {"acctOnlineBal":"14326.95","billNumber":"                    ","businessName":"支付","businessText":" ","ctpAcctName":"银企直连测试用户专用09","ctpAcctNbr":"1289645306100074893822","ctpBankAddress":"招商银行","ctpBankName":"招商银行","currencyNbr":"10","extendedRemark":"06S136ZB000002001739","fatOrSonAccount":" ","fatOrSonBankAddress":" ","fatOrSonBankName":" ","fatOrSonCompanyName":" ","infoFlag":"1","loanCode":"D","mchOrderNbr":"                                                  ","remarkTextClt":"用途 测试转账账号子单元","requestNbr":"7782008731","reversalFlag":" ","textCode":"CPAA","transAmount":"-0.10","transDate":"20241010","transSequenceIdn":"C0146ZD0000N7WZ","transTime":"152858","valueDate":"20241010","virtualNbr":" ","yurRef":"202410101110007"},
             *                                  {"acctOnlineBal":"14326.85","billNumber":"                    ","businessName":"支付","businessText":" ","ctpAcctName":"银企直连测试用户专用09","ctpAcctNbr":"1289645306100074893822","ctpBankAddress":"招商银行","ctpBankName":"招商银行","currencyNbr":"10","extendedRemark":"06S136ZE000003000849","fatOrSonAccount":" ","fatOrSonBankAddress":" ","fatOrSonBankName":" ","fatOrSonCompanyName":" ","infoFlag":"1","loanCode":"D","mchOrderNbr":"                                                  ","remarkTextClt":"用途 测试转账账号子单元","requestNbr":"7782008732","reversalFlag":" ","textCode":"CPAA","transAmount":"-0.10","transDate":"20241012","transSequenceIdn":"C0146ZF0002KM5Z","transTime":"143129","valueDate":"20241012","virtualNbr":" ","yurRef":"202410121110001"},
             *                                  {"acctOnlineBal":"14326.75","billNumber":"                    ","businessName":"支付","businessText":" ","ctpAcctName":"银企直连测试用户专用09","ctpAcctNbr":"1289645306100074893822","ctpBankAddress":"招商银行","ctpBankName":"招商银行","currencyNbr":"10","extendedRemark":"06S136ZE000004000874","fatOrSonAccount":" ","fatOrSonBankAddress":" ","fatOrSonBankName":" ","fatOrSonCompanyName":" ","infoFlag":"1","loanCode":"D","mchOrderNbr":"                                                  ","remarkTextClt":"用途 测试转账账号子单元","requestNbr":"7782008733","reversalFlag":" ","textCode":"CPAA","transAmount":"-0.10","transDate":"20241012","transSequenceIdn":"C0146ZF0002KQUZ","transTime":"145041","valueDate":"20241012","virtualNbr":" ","yurRef":"202410121110002"},
             *                                  {"acctOnlineBal":"14326.65","billNumber":"                    ","businessName":"支付","businessText":" ","ctpAcctName":"银企直连测试用户专用09","ctpAcctNbr":"1289645306100074893822","ctpBankAddress":"招商银行","ctpBankName":"招商银行","currencyNbr":"10","extendedRemark":"06S136ZE000003000853","fatOrSonAccount":" ","fatOrSonBankAddress":" ","fatOrSonBankName":" ","fatOrSonCompanyName":" ","infoFlag":"1","loanCode":"D","mchOrderNbr":"                                                  ","remarkTextClt":"用途 测试转账账号子单元","requestNbr":"7782008734","reversalFlag":" ","textCode":"CPAA","transAmount":"-0.10","transDate":"20241012","transSequenceIdn":"C0146ZF0003NG8Z","transTime":"145557","valueDate":"20241012","virtualNbr":" ","yurRef":"202410121110003"},
             *                                  {"acctOnlineBal":"14286.65","billNumber":"                    ","businessName":"代发其他","businessText":" ","ctpAcctNbr":" ","ctpBankAddress":" ","ctpBankName":" ","currencyNbr":"10","extendedRemark":"4001V284ZK          ","fatOrSonAccount":" ","fatOrSonBankAddress":" ","fatOrSonBankName":" ","fatOrSonCompanyName":" ","infoFlag":"1","loanCode":"D","mchOrderNbr":"                                                  ","remarkTextClt":"直联免前置 代发测试","requestNbr":"4C20002159","reversalFlag":" ","textCode":"AGSH","transAmount":"-40.00","transDate":"20241012","transSequenceIdn":"C0146ZF0003NPEZ","transTime":"152122","valueDate":"20241012","virtualNbr":" ","yurRef":"20241012000000002"},
             *                                  {"acctOnlineBal":"14246.65","billNumber":"                    ","businessName":"代发其他","businessText":" ","ctpAcctNbr":" ","ctpBankAddress":" ","ctpBankName":" ","currencyNbr":"10","extendedRemark":"M001VHG4ZK          ","fatOrSonAccount":" ","fatOrSonBankAddress":" ","fatOrSonBankName":" ","fatOrSonCompanyName":" ","infoFlag":"1","loanCode":"D","mchOrderNbr":"                                                  ","remarkTextClt":"直联免前置 代发测试","requestNbr":"4C20002158","reversalFlag":" ","textCode":"AGSH","transAmount":"-40.00","transDate":"20241013","transSequenceIdn":"C0146ZG00016K6Z","transTime":"001002","valueDate":"20241013","virtualNbr":" ","yurRef":"20241012000000001"},
             *                                  {"acctOnlineBal":"14246.55","billNumber":"                    ","businessName":"支付","businessText":" ","ctpAcctName":"银企直连测试用户专用09","ctpAcctNbr":"1289645306100074893822","ctpBankAddress":"招商银行","ctpBankName":"招商银行","currencyNbr":"10","extendedRemark":"06S136ZE000003001118","fatOrSonAccount":" ","fatOrSonBankAddress":" ","fatOrSonBankName":" ","fatOrSonCompanyName":" ","infoFlag":"1","loanCode":"D","mchOrderNbr":"                                                  ","remarkTextClt":"用途 测试转账账号子单元","requestNbr":"7782008735","reversalFlag":" ","textCode":"CPAA","transAmount":"-0.10","transDate":"20241014","transSequenceIdn":"C0146ZH0001D5VZ","transTime":"133927","valueDate":"20241014","virtualNbr":" ","yurRef":"202410141110001"},
             *                                  {"acctOnlineBal":"14246.45","billNumber":"                    ","businessName":"支付","businessText":" ","ctpAcctName":"银企直连测试用户专用09","ctpAcctNbr":"1289645306100074893822","ctpBankAddress":"招商银行","ctpBankName":"招商银行","currencyNbr":"10","extendedRemark":"06S136ZE000004001117","fatOrSonAccount":" ","fatOrSonBankAddress":" ","fatOrSonBankName":" ","fatOrSonCompanyName":" ","infoFlag":"1","loanCode":"D","mchOrderNbr":"                                                  ","remarkTextClt":"用途 测试转账账号子单元","requestNbr":"7782008736","reversalFlag":" ","textCode":"CPAA","transAmount":"-0.10","transDate":"20241014","transSequenceIdn":"C0146ZH0001F9DZ","transTime":"143820","valueDate":"20241014","virtualNbr":" ","yurRef":"202410141110002"}
             *                                  ]
             *                   },
             *           "head":{
             *                  "bizcode":"","funcode":"trsQryByBreakPoint","reqid":"2024101714355905704149549","resultcode":"SUC0000","resultmsg":"","rspid":"202410171435595780001cdcserver-6c45d76966-r5mpt","userid":"U006763991"}
             *                  },
             *  "signature":{
             *          "sigdat":"3hqh0VLVFWPGwFZnywI+IQ0h2xsQjf+MB/LyDLE30OiKtTzkFZAtuP3W2BoMsaVUmegZSZtGk6u84QuRLLx4DQ==","sigtim":"20241017143559"
             *          }
             * }
             */
            Object data = response.getData();
            JSONObject body = JsonUtils.convertValue(data.toString(), JSONObject.class).optJSONObject("body");
            JSONArray TRANSQUERYBYBREAKPOINT_Z2 = body.optJSONArray("TRANSQUERYBYBREAKPOINT_Z2");
            return Result.success(TRANSQUERYBYBREAKPOINT_Z2.toString());
        }else {
            return Result.fail(ApiCode.PAY_CMB_BACK_MSG_ERROR, response.getMessage());
        }
    }

    /**
     * 电子回单异步查询
     * @param electronReceiptDTO
     * @return
     */
    @Override
    public Result<Object> getElectronReceipt(ElectronReceiptDTO electronReceiptDTO) {
        DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO = ddwxPaymentCfgInfoService.queryInfoByBizCodeAndPlatform(phpCallbackBizCode,electronReceiptDTO.getSystemPlatform());
        Response response = cmbAssistant.getElectronReceipt(electronReceiptDTO,ddwxPaymentCfgInfoDTO);
        if (response.isSuccess()){
            /**
             * {"response":{
             *              "body":{
             *                      "asycalhdz1":{
             *                              "rtncod":"SUC0000","rtndat":"bsb4qpChAchLFzEV7f5MyVedTGqw+NYEMOc1KQ45Weo=","rtnmsg":"","taskid":"1846819833393422337"}
             *                     },
             *              "head":{
             *                      "bizcode":"","funcode":"ASYCALHD","reqid":"2024101715453104543445327","resultcode":"SUC0000","resultmsg":"","rspid":"202410171545348940001cdcserver-6c45d76966-r5mpt","userid":"U006763991"
             *                      }
             *              },
             * "signature":{
             *              "sigdat":"X8ckN8IVjZGYFXTIOgXe5w/f/2NSdoIjZnMy5ApCugFKjJgpfTNmYakB1/fGMLqC35FkPmJODd00rJTUnv8gbg==","sigtim":"20241017154535"
             *              }
             * }
             */
            Object data = response.getData();
            JSONObject body = JsonUtils.convertValue(data.toString(), JSONObject.class).optJSONObject("body");
            JSONObject asycalhdz1 = body.optJSONObject("asycalhdz1");
            return Result.success(asycalhdz1.toString());
        }else {
            return Result.fail(ApiCode.PAY_CMB_BACK_MSG_ERROR, response.getMessage());
        }
    }

    /**
     * 异步打印结果查询
     * @param electronReceiptResultDTO
     * @return
     */
    @Override
    public Result<Object> getElectronReceiptResult(ElectronReceiptResultDTO electronReceiptResultDTO) {
        DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO = ddwxPaymentCfgInfoService.queryInfoByBizCodeAndPlatform(phpCallbackBizCode,electronReceiptResultDTO.getSystemPlatform());
        Response response = cmbAssistant.getElectronReceiptResult(electronReceiptResultDTO,ddwxPaymentCfgInfoDTO);
        if (response.isSuccess()){
            /**
             * {"response":{
             *              "body":{
             *                      "fileurl":"http://s3gw.cmburl.cn:8081/s/L2x4NTUwMS10YXNremlwLXVhdC8xODQ2ODIwNzQ4Njg4NDAwMzg2LnppcD9BV1NBY2Nlc3NLZXlJZD1seDU1MDEtcHJ0c3ZyLXVhdCZFeHBpcmVzPTE3Mjk0MTIyNzAmU2lnbmF0dXJlPXBUczM0MTltbUZYeWlHZUc3azhKeFl5ZFBpUSUzRCZyZXNwb25zZS1jb250ZW50LWRpc3Bvc2l0aW9uPWF0dGFjaG1lbnQlM0JmaWxlbmFtZSUzRDE4NDY4MjA3NDg2ODg0MDAzODYuemlw/mjtPkWiKto2dPLI6mm99RdL_6zI=/KS-lx5501-prtsvr-uat-dmz/0","fintim":"2024-10-17 15:49:15"
             *                      },
             *              "head":{
             *                      "bizcode":"","funcode":"DCTASKID","reqid":"2024101716175000183723194","resultcode":"SUC0000","resultmsg":"","rspid":"202410171617502760001cdcserver-6485f7bb67-zcmt4","userid":"U006763991"
             *                      }
             *             },
             * "signature":{
             *              "sigdat":"OOQUBRPSdxzyI4/iTzJvV1fXT8s9TuIxVfn7/RsrXI3t9RZ2iCN3LnyPW743RO/H8nmQdhPosSds4W9qQveLwg==","sigtim":"20241017161750"
             *              }
             * }
             */
            Object data = response.getData();
            JSONObject body = JsonUtils.convertValue(data.toString(), JSONObject.class).optJSONObject("body");
            Map<String,String> res = new HashMap<>();
            //"fileurl":"http://s3gw.cmburl.cn:8081/   >>> 返回结果  "fileurl":"http:\/\/s3gw.cmburl.cn:8081\/修改
            res.put("fileurl",body.optString("fileurl"));
            return Result.success(res);
        }else {
            return Result.fail(ApiCode.PAY_CMB_BACK_MSG_ERROR, response.getMessage());
        }
    }

    /**
     *代发批次查询
     * @param releasePayBatchDTO
     * @return
     */
    @Override
    public Result<Object> getReleasePayBatch(ReleasePayBatchDTO releasePayBatchDTO) {
        DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO = ddwxPaymentCfgInfoService.queryInfoByBizCodeAndPlatform(phpCallbackBizCode,releasePayBatchDTO.getSystemPlatform());
        Response response = cmbAssistant.getReleasePayBatch(releasePayBatchDTO,ddwxPaymentCfgInfoDTO);
        if (response.isSuccess()){
            /**
             * {"head":{
             *          "funcode":"BB6BTHQY","bizcode":"","rspid":"202410191513291260001cdcserver-5dc584c49d-jp659","resultmsg":"","resultcode":"SUC0000","userid":"U006763991","reqid":"2024101915132800146828144"
             *          },
             *  "body":{
             *          "bb6bthqyz1":[
             *                          {
             *                              "totamt":"40.00","lgnnam":"银企直连测试用户专用09B","agctyp":"M","reqsta":"FIN","oprdat":"20241018","stscod":"E","dmanbr":" ","trstyp":"BYBK","buscod":"N03020","yurref":"202410181847174327028592642","accnam":"银企直连测试用户专用09","ccynbr":"10","trsnum":"2","bbknbr":"75","busmod":"S3015","trsset":"AGF6ZL0300GXKHT","sigacc":"N","trsreq":"C0146ZL0003CC4Z","nusage":"2024-10-18 代发批量支付经办一次调试","errdsp":" ","rtnflg":"S","reqnbr":"4C20002716","athflg":"N","epttim":"000000","eptdat":"20241018","usrnam":"13696087532执行","seqcod":"B001V8M4ZK","sucnum":"2","accnbr":"128964530610006","sucamt":"40.00"
             *                          },
             *                          {
             *                              "totamt":"40.00","lgnnam":"银企直连测试用户专用09B","agctyp":"M","reqsta":"FIN","oprdat":"20241018","stscod":"E","dmanbr":" ","trstyp":"BYBK","buscod":"N03020","yurref":"202410181847200164318720002","accnam":"银企直连测试用户专用09","ccynbr":"10","trsnum":"2","bbknbr":"75","busmod":"S3015","trsset":"AGF6ZL11006FHDT","sigacc":"N","trsreq":"C0146ZL0003D69Z","nusage":"2024-10-18 代发批量支付经办一次调试","errdsp":" ","rtnflg":"S","reqnbr":"4C20002717","athflg":"N","epttim":"000000","eptdat":"20241018","usrnam":"13696087532执行","seqcod":"2001V1W4ZK","sucnum":"2","accnbr":"128964530610006","sucamt":"40.00"
             *                          },
             *                          {
             *                              "totamt":"40.00","lgnnam":"银企直连测试用户专用09B","agctyp":"M","reqsta":"FIN","oprdat":"20241018","stscod":"E","dmanbr":" ","trstyp":"BYBK","buscod":"N03020","yurref":"202410181847209885624872961","accnam":"银企直连测试用户专用09","ccynbr":"10","trsnum":"2","bbknbr":"75","busmod":"S3015","trsset":"AGF6ZL0400GXKHT","sigacc":"N","trsreq":"C0146ZL0002LSLZ","nusage":"2024-10-18 代发批量支付经办一次调试","errdsp":" ","rtnflg":"S","reqnbr":"4C20002718","athflg":"N","epttim":"000000","eptdat":"20241018","usrnam":"13696087532执行","seqcod":"K001V2Y4ZK","sucnum":"2","accnbr":"128964530610006","sucamt":"40.00"
             *                          }
             *                        ]
             *           }
             * }
             */
            Object data = response.getData();
            JSONObject body = JsonUtils.convertValue(data.toString(), JSONObject.class).optJSONObject("body");
            JSONArray bb6bthqyz1 = body.optJSONArray("bb6bthqyz1");
            return Result.success(bb6bthqyz1.toString());
        }else {
            return Result.fail(ApiCode.PAY_CMB_BACK_MSG_ERROR, response.getMessage());
        }
    }


    /**
     * 代发明细查询
     * @param releasePayDetailDTO
     * @return
     */
    @Override
    public Result<Object> getReleasePayDetail(ReleasePayDetailDTO releasePayDetailDTO) {
        DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO = ddwxPaymentCfgInfoService.queryInfoByBizCodeAndPlatform(phpCallbackBizCode,releasePayDetailDTO.getSystemPlatform());
        Response response = cmbAssistant.getReleasePayDetail(releasePayDetailDTO,ddwxPaymentCfgInfoDTO);
        if (response.isSuccess()){
            /**
             *{"head":{
             *          "funcode":"BB6DTLQY","bizcode":"","rspid":"202410191540443090001cdcserver-5dc584c49d-jp659","resultmsg":"","resultcode":"SUC0000","userid":"U006763991","reqid":"2024101915404303356781863"
             *        },
             * "body":{
             *          "bb6dtlqyz1":[
             *                          {
             *                              "trsdsp":"网上代发代扣","digflg":"N","stscod":"S","trsset":"AGF6ZL0800ND1TT","trssqn":"R1526ZL00004ISZ","trxseq":"00001","bthnbr":"B001V8M4ZK","rcvbnk":"755","accnbr":"6214837811201866","trsdat":"20241018","trsamt":"20.00","accnam":"殷萌涵"
             *                          },
             *                          {
             *                              "trsdsp":"网上代发代扣","digflg":"N","stscod":"S","trsset":"AGF6ZL0200ND1UT","trssqn":"R1626ZL00017NOZ","trxseq":"00002","bthnbr":"B001V8M4ZK","rcvbnk":"105","accnbr":"6214837811201973","trsdat":"20241018","trsamt":"20.00","accnam":"鲁贺滢"
             *                          }
             *                       ]
             *        }
             * }
             */
            Object data = response.getData();
            JSONObject body = JsonUtils.convertValue(data.toString(), JSONObject.class).optJSONObject("body");
            JSONArray bb6dtlqyz1 = body.optJSONArray("bb6dtlqyz1");
            return Result.success(bb6dtlqyz1.toString());
        }else {
            return Result.fail(ApiCode.PAY_CMB_BACK_MSG_ERROR, response.getMessage());
        }
    }

    /**
     * 代发明细对账单查询请求
     * @param releasePayReconciliationDTO
     * @return
     */
    @Override
    public Result<Object> getReleasePayReconciliation(ReleasePayReconciliationDTO releasePayReconciliationDTO) {
        DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO = ddwxPaymentCfgInfoService.queryInfoByBizCodeAndPlatform(phpCallbackBizCode,releasePayReconciliationDTO.getSystemPlatform());
        Response response = cmbAssistant.getReleasePayReconciliation(releasePayReconciliationDTO,ddwxPaymentCfgInfoDTO);
        if (response.isSuccess()){
            /**
             *
             */
            Object data = response.getData();
            JSONObject body = JsonUtils.convertValue(data.toString(), JSONObject.class).optJSONObject("body");
            return Result.success(body.toString());
        }else {
            return Result.fail(ApiCode.PAY_CMB_BACK_MSG_ERROR, response.getMessage());
        }
    }

    /**
     * 代发明细对账单处理结果查询请求
     * @param releasePayReconciliationResultDTO
     * @return
     */
    @Override
    public Result<Object> getReleasePayReconciliationResult(ReleasePayReconciliationResultDTO releasePayReconciliationResultDTO) {
        DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO = ddwxPaymentCfgInfoService.queryInfoByBizCodeAndPlatform(phpCallbackBizCode,releasePayReconciliationResultDTO.getSystemPlatform());
        Response response = cmbAssistant.getReleasePayReconciliationResult(releasePayReconciliationResultDTO,ddwxPaymentCfgInfoDTO);
        if (response.isSuccess()){
            /**
             *
             */
            Object data = response.getData();
            JSONObject body = JsonUtils.convertValue(data.toString(), JSONObject.class).optJSONObject("body");
            body.optString("fileurl");
            Map<String,String> res = new HashMap<>();
            res.put("fileurl",body.optString("fileurl"));
            return Result.success(res);
        }else {
            return Result.fail(ApiCode.PAY_CMB_BACK_MSG_ERROR, response.getMessage());
        }
    }

    @Override
    public Result<Object> getEBankReconciliationPending(EBankReconciliationPendingDTO eBankReconciliationPendingDTO) {
        DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO = ddwxPaymentCfgInfoService.queryInfoByBizCodeAndPlatform(phpCallbackBizCode,eBankReconciliationPendingDTO.getSystemPlatform());
        Response response = cmbAssistant.getEBankReconciliationPending(eBankReconciliationPendingDTO,ddwxPaymentCfgInfoDTO);
        if (response.isSuccess()){
            /**
             *"body":{"ntnstqryx":[{"endmth":"202409","yermth":"202409","ectflg":"1","cntflg":"Y"}]}
             */
            Object data = response.getData();
            JSONObject body = JsonUtils.convertValue(data.toString(), JSONObject.class).optJSONObject("body");
            //JSONArray ntnstqryx = body.optJSONArray("ntnstqryx");
            return Result.success(body.toString());
        }else {
            return Result.fail(ApiCode.PAY_CMB_BACK_MSG_ERROR, response.getMessage());
        }
    }

    @Override
    public Result<Object> getEBankReconciliationHandling(EBankReconciliationHandlingDTO eBankReconciliationHandlingDTO) {
        DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO = ddwxPaymentCfgInfoService.queryInfoByBizCodeAndPlatform(phpCallbackBizCode,eBankReconciliationHandlingDTO.getSystemPlatform());
        Response response = cmbAssistant.getEBankReconciliationHandling(eBankReconciliationHandlingDTO,ddwxPaymentCfgInfoDTO);
        if (response.isSuccess()){
            /**
             *
             */
            Object data = response.getData();
            JSONObject body = JsonUtils.convertValue(data.toString(), JSONObject.class).optJSONObject("body");
            return Result.success(body.toString());
        }else {
            return Result.fail(ApiCode.PAY_CMB_BACK_MSG_ERROR, response.getMessage());
        }
    }

    @Override
    public Result<Object> getEBankReconciliationBusiness(EBankReconciliationBusinessDTO eBankReconciliationBusinessDTO) {
        DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO = ddwxPaymentCfgInfoService.queryInfoByBizCodeAndPlatform(phpCallbackBizCode,eBankReconciliationBusinessDTO.getSystemPlatform());
        Response response = cmbAssistant.getEBankReconciliationBusiness(eBankReconciliationBusinessDTO,ddwxPaymentCfgInfoDTO);
        if (response.isSuccess()){
            /**
             *
             */
            Object data = response.getData();
            JSONObject body = JsonUtils.convertValue(data.toString(), JSONObject.class).optJSONObject("body");
            return Result.success(body.toString());
        }else {
            return Result.fail(ApiCode.PAY_CMB_BACK_MSG_ERROR, response.getMessage());
        }
    }

    @Override
    public Result<Object> getEBankReconciliationDetail(EBankReconciliationDetailDTO eBankReconciliationDetailDTO) {
        DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO = ddwxPaymentCfgInfoService.queryInfoByBizCodeAndPlatform(phpCallbackBizCode,eBankReconciliationDetailDTO.getSystemPlatform());
        Response response = cmbAssistant.getEBankReconciliationDetail(eBankReconciliationDetailDTO,ddwxPaymentCfgInfoDTO);
        if (response.isSuccess()){
            /**
             *
             */
            Object data = response.getData();
            JSONObject body = JsonUtils.convertValue(data.toString(), JSONObject.class).optJSONObject("body");
            return Result.success(body.toString());
        }else {
            return Result.fail(ApiCode.PAY_CMB_BACK_MSG_ERROR, response.getMessage());
        }
    }


    /**
     * 企银批量支付批次查询
     * @param paymentBatchDTO
     * @return
     */
    @Override
    public Result<Object> getPaymentBatch(PaymentBatchDTO paymentBatchDTO) {
        DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO = ddwxPaymentCfgInfoService.queryInfoByBizCodeAndPlatform(phpCallbackBizCode,paymentBatchDTO.getSystemPlatform());
        Response response = cmbAssistant.getPaymentBatch(paymentBatchDTO,ddwxPaymentCfgInfoDTO);
        if (response.isSuccess()){
            /**
             *
             */
            Object data = response.getData();
            JSONObject body = JsonUtils.convertValue(data.toString(), JSONObject.class).optJSONObject("body");
            return Result.success(body.toString());
        }else {
            return Result.fail(ApiCode.PAY_CMB_BACK_MSG_ERROR, response.getMessage());
        }
    }

    /**
     * 企银批量支付明细查询
     * @param paymentDetailDTO
     * @return
     */
    @Override
    public Result<Object> getPaymentDetail(PaymentDetailDTO paymentDetailDTO) {
        DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO = ddwxPaymentCfgInfoService.queryInfoByBizCodeAndPlatform(phpCallbackBizCode,paymentDetailDTO.getSystemPlatform());
        Response response = cmbAssistant.getPaymentDetail(paymentDetailDTO,ddwxPaymentCfgInfoDTO);
        if (response.isSuccess()){
            /**
             *
             */
            Object data = response.getData();
            JSONObject body = JsonUtils.convertValue(data.toString(), JSONObject.class).optJSONObject("body");
            return Result.success(body.toString());
        }else {
            return Result.fail(ApiCode.PAY_CMB_BACK_MSG_ERROR, response.getMessage());
        }
    }


    /**
     * 重复调用
     * @param repeatInvokingDTO
     * @return
     */
    @Override
    public Result<Object> repeatInvoking(RepeatInvokingDTO repeatInvokingDTO) {
        String orderNo = repeatInvokingDTO.getPayOrderNum();
        DdwxPaymentMessageInfoDTO msgInfo = ddwxPaymentMessageInfoService.findByPayOrderNumber(orderNo);
        DdwxPaymentCfgInfoDTO ddwxPaymentCfgInfoDTO = ddwxPaymentCfgInfoService.queryInfoByBizCodeAndPlatform(phpCallbackBizCode,repeatInvokingDTO.getSystemPlatform());

        Result result = new Result(ApiCode.CMB,JacksonUtil.parse(msgInfo.getPushMsg(),Map.class));
        String noticeBodyJsonStr = JsonUtils.toJson(result);
        //增加签名信息
        noticeBodyJsonStr = addRequestSignature(noticeBodyJsonStr);
        String notifyUrl = ddwxPaymentCfgInfoDTO.getNotifyUrl();
        int pushStatus = 0;
        try {
            log.info("cmb repeatInvoking parameter：{}", noticeBodyJsonStr);
            String resultMsg = HttpClientUtil.postJSON(notifyUrl, null, noticeBodyJsonStr);
            log.info("cmb repeatInvoking result：{}", resultMsg);
            // 转义结果
            Result rts = JacksonUtil.parse(resultMsg, Result.class);
            if (Objects.nonNull(rts) && rts.getCode() == ApiCode.OK.getCode().intValue()) {
                pushStatus = CmbPayStatusEnums.succeeded.getCode();
            } else {
                pushStatus = CmbPayStatusEnums.failed.getCode();
            }
        } catch (Exception ex) {
            log.error("cmb repeatInvoking 支付回调异常", ex);
            pushStatus = CmbPayStatusEnums.failed.getCode();
        } finally {
            msgInfo.setVersion(msgInfo.getVersion()+1);
            msgInfo.setPushStatus(pushStatus);
            ddwxPaymentMessageInfoService.updateById(msgInfo);
        }
        return Result.success("重复调用成功!");
    }

    private String addRequestSignature(String noticeBodyJsonStr) {
        // jsonString转换成Map
        /*Map<String, String> jsonMap = JSON.parseObject(noticeBodyJsonStr, new TypeReference<HashMap<String, String>>() {
        });*/
        Map jsonMap =JacksonUtil.parse(noticeBodyJsonStr,Map.class);
        Map<String,String> lastMap = changeString(jsonMap);
        //System.out.println(jsonMap.toString());
        SignMd5Utils signMd5Utils = new SignMd5Utils();
        log.info("cmb notify post sign：{}", lastMap.toString());
        String sign = signMd5Utils.createSign(lastMap, key, "UTF-8");
        lastMap.put("sign",sign);
        log.info("cmb notify post sign：{}", sign);
        return JacksonUtil.getNormalPatternJsonString(lastMap);
    }

    private static final String key = "UVIUODIBDU708";  //MD5密钥
    public static Map<String, String> changeString(Map<String,Object> map){
        try {
            Map<String, String> resMap = new HashMap<>();
            for (Map.Entry<String, Object> entry: map.entrySet()) {
                String key1 = entry.getKey();
                Object obj = entry.getValue();
                //新建一个 ObjectMapper 对象
                ObjectMapper objectMapper = new ObjectMapper();
                if (obj instanceof String) {
                    resMap.put(key1, obj.toString());
                    continue;
                }
                String value = objectMapper.writeValueAsString(obj);
                resMap.put(key1, value);
            }
            return resMap;
        } catch (Exception e) {
            log.error("CmbAbstractEventTypeResponse Map error", e);
        }
        return null;
    }

}
